package bs.app.Client;

import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.HTTPConnection.CConnection;
import bs.app.Main.CCanvas;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;
import bs.app.android.Image;

public class AppPage5 {
	public int percentOfScroll = 0;
	public String[] Name, Title, Date;
	public String[] contentparent;
	public String[][] subcontent;
	public Image[] ImageServer;
	public int xName, yName, xTitle, yTitle, xDate, yDate, xstartBackground,
			ystartBackground;
	public int xcontent, ycontent;
	public int xstart, ystart;
	public int yspaceBrTag;
	public int yspaceworld;
	public int VCryContent = 0;
	public int[] heightContent;
	public int distance = 0;
	public int Cry = 0, AmDuong = 0;
	public final int NumberDivided = 2;
	public boolean isScroll = false;
	public boolean PressOnce = false;
	public int xscroll, yscroll, xscrollpoint, yscrollpoint;
	public int distancescrollpoint = 0;

	public int index = 0;
	public int solan = 0;
	public int vitriloadhinh = 0;

	public int ytranslate = 0;
	public int StringHeight = 0;
	public int ytranslatePointScroll = 0;
	public int ytranslatePointer = 0;
	public int sodu = 0;
	public int tempdistansce = 0;

	public AppPage5() {
		if (CRess.scrollbar == null)
			CRess.LoadImagePage2(new Image[0]);
		xscroll = CCanvas.xTranslate + CCanvas.s_iWcanvas - CRess.scrollbar[0].getWidth();
		yscroll = CCanvas.yTranslate + 40; // 50
		xscrollpoint = xscroll + 1;
		yscrollpoint = yscroll + 1;
		ytranslate = 20;
	}

	public void init(String[] data, Image[] img) {

		VCryContent = 0;
		ytranslatePointer = 0;
		ytranslatePointScroll = 0;
		percentOfScroll = 0;
		contentparent = null;

		CCanvas.s_Instan.connect.isPainting = false;
		ImageServer = img;
		xName = xDate = xTitle = xcontent = CCanvas.xTranslate + 13;
		if (!data[0].trim().equals(""))
			Name = CCanvas.mediumfont.splitStrInLine(data[0], CCanvas.s_iWcanvas - xName - 15);
		if (!data[3].trim().equals(""))
			Date = CCanvas.mediumfont.splitStrInLine(data[3], CCanvas.s_iWcanvas - xDate - 15);
		if (!data[1].trim().equals(""))
			Title = CCanvas.mediumfont.splitStrInLine(data[1], CCanvas.s_iWcanvas - xTitle - 15);
	
		
		String contentdata = data[4];
		String[] brtag = null;
		brtag = CConnection.split(contentdata, "<br/>");
		int indexdata = 0;
		contentparent = null;
		for (int i = 0; i < brtag.length; i++) {
			if (!brtag[i].trim().equals("")) {
				// ko lay record co khoang trang
				contentparent = PageManager.insertArray(contentparent, brtag[i]);
			}
		}
		subcontent = new String[contentparent.length][];
		heightContent = new int[contentparent.length];
		int value = 0;
		int temp = 0;
		for (int i = 0; i < subcontent.length; i++) {
			int begin = contentparent[i].indexOf("img ");
			if (begin > 0) {
				subcontent[i] = CCanvas.smallfont.splitStrInLine("#*!@", CCanvas.xTranslate + CCanvas.s_iWcanvas - xName - 5); //namnv -5
			} else {
				subcontent[i] = CCanvas.smallfont.splitStrInLine(contentparent[i], CCanvas.xTranslate + CCanvas.s_iWcanvas - xName - 5); //namnv -5
			}
		}
		String[] filteredword = CConnection.split(subcontent[subcontent.length - 1][0], "\n");
		if (filteredword != null)
			subcontent[subcontent.length - 1][0] = filteredword[0];

		int heighttem = 0;
		int savei[] = new int[subcontent.length];
		int index = 0;
		int htext = 0;
		yspaceworld = 18;
		for (int i = 0; i < subcontent.length; i++) {
			if (i == 0) {
				value = 0;
				if (subcontent[i][0].trim().equals("#*!@")) // da nhin thay hinh roi
				{
					int sodu = 0;
					if (ImageServer[index].getHeight() % yspaceworld != 0)
						sodu = 1;
					heighttem = (ImageServer[index].getHeight() / yspaceworld) + sodu;
					savei[i] = heighttem;
					index++;
				}

				for (int j = 0; j < savei.length; j++) {
					if (i > j) {
						temp += savei[j];
					}
				}
				value = temp;
				temp = 0;
			} else {
				if (subcontent[i][0].trim().equals("#*!@")) // da nhin thay hinh
															// roi
				{
					int sodu = 0;
					if (ImageServer[index].getHeight() % yspaceworld != 0)
						sodu = 1;
					heighttem = (ImageServer[index].getHeight() / yspaceworld)
							+ sodu;
					savei[i] = heighttem;
					index++;
				}
				for (int k = 0; k < i; k++) // danh cho nhung doan test phia
											// tren hinh
				{
					if (savei[k] == 0)
						temp += subcontent[k].length;
				}
				for (int j = 0; j < savei.length; j++) {
					if (i > j) {
						temp += savei[j];
					}
				}
				value = temp;
				temp = 0;
			}
			heightContent[i] = value;
		}
		yName = CCanvas.yTranslate + 40;
		if (Name != null)
			yDate = yName + Name.length * yspaceworld;
		if (Date != null)
			yTitle = yDate + Date.length * yspaceworld;
		if (Title != null)
			ycontent = yTitle + Title.length * yspaceworld;
		else
			ycontent = yTitle + 4;

		for (int k = 0; k < subcontent[subcontent.length - 1].length; k++) {
			if (subcontent[subcontent.length - 1][k].equals("#*!@")) {
				StringHeight = ycontent + ImageServer[index].getHeight()+ (heightContent[subcontent.length - 1] * yspaceworld)+ (subcontent.length - 1) * 5;
				index++;
			} else {
				StringHeight = ycontent + k * yspaceworld + (heightContent[subcontent.length - 1] * yspaceworld)+ (subcontent.length - 1) * 5;
			}
		}
//		StringHeight+=CCanvas.s_iHcanvas/10;
		CCanvas.resetPointerPress();
	}

	public void ReLoadImage(Image[] img) {
		ImageServer = img;
		int heighttem = 0;
		int savei[] = new int[subcontent.length];
		int index = 0;
		int htext = 0;
		yspaceworld = 18;
		int value = 0;
		int temp = 0;
		for (int i = 0; i < subcontent.length; i++) {
			if (i == 0) {
				value = 0;
				if (subcontent[i][0].trim().equals("#*!@")) // da nhin thay hinh  roi
				{
					int sodu = 0;
					if (ImageServer[index].getHeight() % yspaceworld != 0)
						sodu = 1;
					heighttem = (ImageServer[index].getHeight() / yspaceworld)
							+ sodu;
					savei[i] = heighttem;
					index++;
				}

				for (int j = 0; j < savei.length; j++) {
					if (i > j) {
						temp += savei[j];
					}
				}
				value = temp;
				temp = 0;
			} else {
				if (subcontent[i][0].trim().equals("#*!@")) // da nhin thay hinh roi
				{
					int sodu = 0;
					if (ImageServer[index].getHeight() % yspaceworld != 0)
						sodu = 1;
					heighttem = (ImageServer[index].getHeight() / yspaceworld)
							+ sodu;
					savei[i] = heighttem;
					index++;
				}
				for (int k = 0; k < i; k++) // danh cho nhung doan test phia tren hinh
				{
					if (savei[k] == 0)
						temp += subcontent[k].length;
				}
				for (int j = 0; j < savei.length; j++) {
					if (i > j) {
						temp += savei[j];
					}
				}
				value = temp;
				temp = 0;
			}
			heightContent[i] = value;
		}
		yName = CCanvas.yTranslate + 40;
		if (Name != null)
			yDate = yName + Name.length * yspaceworld;
		if (Date != null)
			yTitle = yDate + Date.length * yspaceworld;
		if (Title != null)
			ycontent = yTitle + Title.length * yspaceworld;
		else
			ycontent = yTitle + 4;

		for (int k = 0; k < subcontent[subcontent.length - 1].length; k++) {
			if (subcontent[subcontent.length - 1][k].equals("#*!@")) {
				StringHeight = ycontent + ImageServer[index].getHeight()
						+ (heightContent[subcontent.length - 1] * yspaceworld)
						+ (subcontent.length - 1) * 5;
				index++;
			} else {
				StringHeight = ycontent + k * yspaceworld
						+ (heightContent[subcontent.length - 1] * yspaceworld)
						+ (subcontent.length - 1) * 5;
			}
		}
//		StringHeight+=CCanvas.s_iHcanvas/10;
	}

	public void Paint(Graphics g) {
		paintBackGround(g);
		//g.setClip(0, yName - 2, CCanvas.s_iWcanvas, CCanvas.yTranslate+ CRess.background.getHeight() - yName - 3);
		g.setClip(0, yName - 2, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND);
		if (Name != null)
			for (int i = 0; i < Name.length; i++) {
				if (!Name[i].trim().equals("") && Name[i] != null) {
//					if (CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(),g.getClipWidth(), g.getClipHeight(), xName, yName + i * yspaceworld + VCryContent, CCanvas.s_iWcanvas,yspaceworld)) 
					{
						CCanvas.mediumfont.drawString(g, Name[i], xName, yName + i * yspaceworld + VCryContent, 0, 0xced5ce,0x080ca6, true);
					}
				}
			}
		if (Date != null)
			for (int i = 0; i < Date.length; i++) {
				if (!Date[i].trim().equals("") && Date[i] != null) {
//					if (CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight(), xDate, yDate + i * yspaceworld + VCryContent, CCanvas.s_iWcanvas, yspaceworld)) 
					{
						CCanvas.smallfont.drawString(g, Date[i], xDate, yDate + i * yspaceworld + VCryContent, 0, 0x8a8c8b,0x080ca6, false);
					}
				}
			}
		if (Title != null)
			for (int i = 0; i < Title.length; i++) {
				if (!Title[i].trim().equals("") && Title[i] != null) {
//					if (CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight(), xTitle, yTitle + i * yspaceworld + VCryContent, 240,yspaceworld))
						CCanvas.mediumfont.drawString(g, Title[i], xTitle, yTitle + i * yspaceworld + VCryContent, 0,0xced5ce, 0x080ca6, false);
				}
			}
		if (subcontent != null)
			for (int i = 0; i < subcontent.length; i++) {
				for (int k = 0; k < subcontent[i].length; k++) {
					if (subcontent[i][k].equals("#*!@")) {
						if (ImageServer[index] != null) {
//							if (CCanvas.s_Instan.ispaint(g.getClipX(),g.getClipY(), g.getClipWidth(),g.getClipHeight(),CCanvas.s_iWcanvas/ 2 - ImageServer[index].getWidth() / 2, 
//											ycontent + (heightContent[i] * yspaceworld) + i * 5 + VCryContent,ImageServer[index].getWidth(), ImageServer[index].getHeight())) 
							{
								g.drawImage(ImageServer[index], CCanvas.s_iWcanvas / 2 - ImageServer[index].getWidth()/ 2,
										ycontent + (heightContent[i] * yspaceworld) + i * 5 + VCryContent, Graphics.TOP | Graphics.LEFT);
							}
						}
						index++;
					} else {
//						if (CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight(), xcontent, ycontent + k * yspaceworld + (heightContent[i] * yspaceworld) + i * 5 + VCryContent, CCanvas.s_iWcanvas, yspaceworld)) 
						{
							CCanvas.smallfont.drawString(g, subcontent[i][k], xcontent, ycontent + k * yspaceworld + (heightContent[i] * yspaceworld)
											+ i * 5 + VCryContent, 0, 0xced5ce, 0xffffff, false);
						}
					}
				}
			}
		index = 0;
		g.setClip(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
		
		g.drawImage(CRess.scrollbar[0], xscroll, yscroll, Graphics.TOP | Graphics.LEFT);
		int i=yscroll + CRess.scrollbar[0].getHeight();
		for(; i < CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND - CConfig.HIEGHT_TITLE - CRess.scrollbar[2].getHeight(); i+=CRess.scrollbar[1].getHeight())
			g.drawImage(CRess.scrollbar[1], xscroll, i, Graphics.TOP | Graphics.LEFT);
		g.drawImage(CRess.scrollbar[2], xscroll, i, Graphics.TOP | Graphics.LEFT);
		
		g.drawImage(CRess.scrollpoint, xscrollpoint, yscrollpoint + ytranslatePointScroll, Graphics.TOP | Graphics.LEFT);
		CCanvas.mediumfont.drawString(g, "Menu", 5,CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
		CCanvas.mediumfont.drawString(g, "Quay Lại",  CCanvas.s_iWcanvas - CCanvas.mediumfont.stringWidth("Quay Lại") - 3,CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
		CCanvas.s_Instan.connect.isPainting = true;
	}

	public void paintBackGround(Graphics g) {
		g.setColor(0x000000);
		g.fillRect(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
		g.drawImage(CRess.background, CCanvas.s_iWcanvas>>1, CCanvas.s_iHcanvas>>1, CPosition.ANCHOR_HCENTER_VCENTER);
		if(CCanvas.s_iWcanvas > CRess.imgTitle.getWidth()){
			g.drawImage(CRess.imgTitle, 0, 0, CPosition.ANCHOR_LEFT_TOP);
			for(int i = CRess.imgTitle.getWidth(); i < CCanvas.s_iWcanvas + CRess.imgBanner_back.getWidth(); i+=CRess.imgBanner_back.getWidth())
				g.drawImage(CRess.imgBanner_back, i, 0, CPosition.ANCHOR_LEFT_TOP);

		}else{
			g.drawImage(CRess.imgTitle, 0, 0, CPosition.ANCHOR_LEFT_TOP);
		}
		for(int i=0; i < CRess.imgDotted.length; i++)
			g.drawImage(CRess.imgDotted[i], CCanvas.s_iWcanvas - 30 + (i * (CRess.imgDotted[i].getWidth()+5)) , 10, CPosition.ANCHOR_LEFT_TOP);
		paintMenuBar(g);
	}
	public void paintMenuBar(Graphics g){
		if(CCanvas.s_iWcanvas > CRess.menubar.getWidth()){
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);
			for(int i = CRess.menubar.getWidth(); i < CCanvas.s_iWcanvas + CRess.footer_back.getWidth(); i+=CRess.footer_back.getWidth())
				g.drawImage(CRess.footer_back, i, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

		}else{
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM);

		}
	}

	public int pointerItems(int x, int y) {
		int x0 = 0;
		int x1 = CCanvas.s_iWcanvas;
		int y0 = CConfig.HIEGHT_COMMNAND;
		int y1 = CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND;

		if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
			if (y < yscrollpoint + ytranslatePointScroll) {
				CCanvas.keyPressed[KeyConstant.KEY_UP] = true;
			} else {
				CCanvas.keyPressed[KeyConstant.KEY_DOWN] = true;
			}
		}
		return -1;
	}
	
	public void pointerPressed(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) { // left softkey
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 3) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			} else { // click into items
				pointerItems(px, py);
			}
		}
		CCanvas.resetPointerPress(); // User PointerReleased
	}
	//namnv add pointerDragged
	public void pointerDragged(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) { // left softkey
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 3) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			} else { // click into items
				pointerItems(px, py);
			}
		}
		CCanvas.resetPointerPress(); // User PointerReleased
	}
	
	public void pointerReleased(int px, int py) {
		CCanvas.resetPointerPress(); // User PointerReleased
	}

	public void ScrollItem() {
		if (Cry > 0) {
			VCryContent -= (Cry / NumberDivided + Cry % NumberDivided)
					* AmDuong;
			Cry -= (Cry / NumberDivided + Cry % NumberDivided);
		} else {
			Cry = 0;
			PressOnce = false;
		}
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,
			int currentAsccii[]) {
		if (CCanvas.s_Instan.connect.isPainting == true) {
			update();
			if (keyPressed[KeyConstant.KEY_RIGHT]) // User PressKey Fire
			{

			} else if (keyPressed[KeyConstant.KEY_LEFT]) // User PressKey Fire
			{

			} else if (keyPressed[KeyConstant.KEY_UP]
					|| keyHold[KeyConstant.KEY_UP]) // User PressKey Fire
			{
				if (VCryContent < 0) {
					VCryContent += ytranslate;
					percentOfScroll = (VCryContent * 1000)/ ((CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20) - StringHeight);
					ytranslatePointer = (ytranslate * (CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20)) / (StringHeight - 320);
					ytranslatePointScroll = percentOfScroll* ((CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20) - CRess.scrollpoint.getHeight() - 2) / 1000;
				} else {
					ytranslatePointScroll = 0;
					percentOfScroll = 0;
				}

			} else if (keyPressed[KeyConstant.KEY_DOWN]|| keyHold[KeyConstant.KEY_DOWN]) // User PressKey Fire
			{
				if(StringHeight +VCryContent > (CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20)){
					VCryContent-=ytranslate;
					percentOfScroll = (VCryContent*1000)/((CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20)-StringHeight);
					ytranslatePointer=(ytranslate*(CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20))/(StringHeight-CCanvas.s_iHcanvas);
					ytranslatePointScroll = percentOfScroll* ((CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20)-CRess.scrollpoint.getHeight()-2 )/1000;
					//System.out.println("ytranslatePointScroll:"+ ytranslatePointScroll);
					if (ytranslatePointScroll > ((CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20) -CRess.scrollpoint.getHeight()-2 )){
						ytranslatePointScroll = (CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20)-CRess.scrollpoint.getHeight()-2 ;
					}
				}else{
					ytranslatePointScroll =  ((CCanvas.s_iHcanvas - yName - CConfig.HIEGHT_COMMNAND - 20)-CRess.scrollpoint.getHeight()-2 );
					percentOfScroll = 1000;
				}
			} else if (keyPressed[KeyConstant.KEY_FIRE]) // User PressKey Fire
			{

			} else if (keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]) {
				CCanvas.submenu.initCommand();
				if (CCanvas.submenu.leftSoftKey != null) {
					CCanvas.submenu.leftSoftKey.action.perform();
				}
				CCanvas.CurrentScreen = CCanvas.isSubMenu;
			} else if (keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]) {
				CCanvas.s_Instan.LoadingState.timeForLoading = 50;
				CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
				CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;

				if (CCanvas.s_Instan.connect.URL.size() > 0)
					CCanvas.s_Instan.connect.URL.removeElementAt(CCanvas.s_Instan.connect.URL.size() - 1);
				if (CCanvas.s_Instan.connect.Link.size() > 0)
					CCanvas.s_Instan.connect.Link.removeElementAt(CCanvas.s_Instan.connect.Link.size() - 1);

				CCanvas.s_Instan.connect.setURL((String) CCanvas.s_Instan.connect.URL.lastElement());
				String link = (String) CCanvas.s_Instan.connect.URL.lastElement();
				int begin = link.indexOf("&page=") + 6;
				if (begin > 5) {
					int end = link.indexOf("&", begin - 3);
					link = link.substring(begin, end);
					CCanvas.s_Instan.connect.numberpage = Integer.parseInt(link.trim());
				}
				CCanvas.s_Instan.connect.RequestServerGetData();
			}
		}
		CCanvas.resetkey(); // User KeyReleased
	}

	public void update() {
		ScrollItem();
	}
}
